<!doctype html>
<html>
@js(lib/layui/layui.js)
@js(js/jquery.min.js)
@js(js/xadmin.js)
<head>
    <style>
        html {
            overflow-x: hidden;
            overflow-y: hidden;
        }

        #container {
            height: 97%;
            width: 98.5%;
        }

        .input-card {
            display: flex;
            flex-direction: column;
            min-width: 0;
            word-wrap: break-word;
            background-color: #fff;
            background-clip: border-box;
            border-width: 0;
            border-radius: 0.4rem;
            box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
            position: fixed;
            bottom: 1rem;
            right: 1rem;
            flex: 1 1 auto;
            padding: 0.75rem 1.25rem;
            width: 22rem;
        }

        .input-item {
            position: relative;
            display: -ms-flexbox;
            display: flex;
            -ms-flex-wrap: wrap;
            flex-wrap: wrap;
            -ms-flex-align: center;
            align-items: center;
            width: 100%;
            height: 3rem;
        }

        .input-item-text {
            padding-right: 10px;
        }

        .btn {
            top: 1.5rem;
            position: absolute;
            right: 3rem;
            border: 1px solid #ccc;
            padding: 2px 10px 2px 10px;
            border-radius: 5px;
            cursor:pointer;
        }
        .info {
            padding: .3rem .5rem;
            margin-bottom: 1rem;
            border-radius: .25rem;
            position: fixed;
            top: .2rem;
            background-color: white;
            width: auto;
            border-width: 0;
            right: .2rem;
            cursor:pointer;
            font-size: 14px;
            box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
        }
    </style>
</head>
<body>
<div style="height:500px; width:1000px;">
    <div id="container"></div>
    <div class='info' onclick="xadmin.close()">关闭</div>
    <div class="input-card">
        <div class="input-item">
            <div class="input-item-prepend"><span class="input-item-text">经纬度</span></div>
            <input id='lnglat' type="text" value='{{$lonlat}}'>
        </div>
        <span id="regeo" class="btn">确认</span>
    </div>
</div>
<script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
<script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=6e49aeefcee929a0e38315b187c442d2&plugin=AMap.Geocoder"></script>
<script type="text/javascript">
    var lnglated = $('#lnglat').val().split(',');

    //编辑的时候标记点
    if(lnglated[0] && lnglated[1]){
        var map = new AMap.Map("container", {
            resizeEnable: true,
            center: lnglated,
            zoom: 13
        });

        markerOn(lnglated);
    }else{
        var map = new AMap.Map("container", {
            resizeEnable: true,
            zoom: 13
        });
    }

    var geocoder, marker;

    function regeoCode() {
        if (!geocoder) {
            geocoder = new AMap.Geocoder({
                city: "010", //城市设为北京，默认：“全国”
                radius: 1000 //范围，默认：500
            });
        }
        var lnglat = document.getElementById('lnglat').value.split(',');
        if (!marker) {
            marker = new AMap.Marker();
            map.add(marker);
        }
        marker.setPosition(lnglat);

        geocoder.getAddress(lnglat, function(status, result) {
            if (status === 'complete'&&result.regeocode) {
                var address = result.regeocode.formattedAddress;
                console.log(address);
            }else{
                console.log('根据经纬度查询地址失败');
            }
        });
    }

    function markerOn(lnglat){//标记点
        if(!marker){
            marker = new AMap.Marker();
            map.add(marker);
        }
        marker.setPosition(lnglat);
    }

    map.on('click', function (e) {
        document.getElementById('lnglat').value = e.lnglat;
        regeoCode();
    })
    document.getElementById('lnglat').onkeydown = function (e) {
        if (e.keyCode === 13) {
            regeoCode();
            return false;
        }
        return true;
    };

    $('#regeo').on('click', function () {
        var val = $('#lnglat').val().split(',');

        if (val.length != 2) {
            parent.layer.msg('请点击地图确认经纬度');
            return;
        }
        parent.layer.msg('OK', {time: 500});
        parent.$('#longitude').val(val[0]);
        parent.$('#latitude').val(val[1]);
        xadmin.close();
    });

</script>
</body>
</html>